using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.PX.Dto;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// PX_Exam
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class PX_ExamController : CoreApiController
    {
        private readonly PX_ExamService _service;

        public object PX_PlanList { get; private set; }

        /// <summary>
        /// PX_Exam
        ///</summary>
        public PX_ExamController(PX_ExamService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<PX_ExamDto>
                pagingQueryModel)
        {
            //IPageModel<PX_ExamDto>
            //    result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            //retObj.PageData = result.PageData?.Select(a => new
            //{

            //    //a.PlanId,//培训计划ID
            //    a.PX_Plan.Nameoftrainingplan,//培训名称
            //    a.Fullmarks,//满分
            //    a.PX_ExamUser.First().Examinationscore,//得分
            //    a.Examinationstarttime,//考试开始时间
            //    a.Endtimeofexamination,//考试结束时间
            //    a.Status,//状态(未开始/进行/结束)
            //    //a.Examinationmethods,//考试方式
            //    //a.facilityvalue,//考试难易度
            //    //a.Fullmarks,//满分
            //    a.Qualificationscore,//合格分数
            //    //a.JudgmentNumber,//判断题数
            //    //a.JudgmentScore,//判断每题分数
            //    //a.SingleNumber,//单选题数
            //    //a.SingleScore,//单选每题分数
            //    //a.MultipleNumber,//多选题数
            //    //a.MultipleScore,//多选每题分数
            //    //a.Place,//考试地点
            //    //a.IsShow,//是否显示成绩
            //    //a.IsNotice,//是否发送通知
            //    //a.CreateFactory,//隶属工厂
            //    //a.CreateDept,//
            //    //a.UpdateDept,//
            //}
            //).ToList();


            var ojbList = new List<object>();
            for (int i = 0; i < 20; i++)
            {
                ojbList.Add(new
                {
                    Id = Guid.NewGuid(),
                    Nameoftrainingplan = "培训名称",//培训名称
                    Fullmarks = 100,//满分
                    Examinationscore = 60,//得分
                    Qualificationscore = 90,//合格分数
                    Examinationstarttime = DateTime.Now,//考试开始时间
                    Endtimeofexamination = DateTime.Now,//考试结束时间
                    Status = 0,//状态(未开始/进行/结束)
                    StatusText = "未开始"
                });
            }
            retObj.PageData = ojbList;
            //retObj.RowCount = result.RowCount;
            retObj.RowCount = 20;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(PX_ExamDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.PlanId,//培训计划ID
                model.facilityvalue,//考试难易度
                model.Fullmarks,//满分
                model.Qualificationscore,//合格分数
                model.JudgmentNumber,//判断题数
                model.JudgmentScore,//判断每题分数
                model.SingleNumber,//单选题数
                model.SingleScore,//单选每题分数
                model.MultipleNumber,//多选题数
                model.MultipleScore,//多选每题分数
                model.Examinationstarttime,//考试开始时间
                model.Endtimeofexamination,//考试结束时间
                model.Examinationmethods,//考试方式
                model.Place,//考试地点
                model.IsShow,//是否显示成绩
                model.IsNotice,//是否发送通知
                model.Status,//状态(未开始/进行/结束)
                model.CreateFactory,//隶属工厂
                model.CreateDept,//
                model.UpdateDept,//
            };
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 获取考题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetGetQuestions")]
        public IActionResult GetQuestions(Guid id)
        {
            List<object> PX_QuestionsList = new List<object>();
            for (int i = 0; i < 5; i++)
            {
                PX_QuestionsList.Add(new //PX_Questions
                {
                    Id = Guid.NewGuid(),
                    QuestionManagementContent = "题目名称" + i,
                    //PX_QuestionsOptions
                    PX_QuestionsOptions = new List<object>() {

                        new {
                             Id=Guid.NewGuid(),
                             OptionName="A",
                             OptionComment="选项内容A",
                        },
                        new {
                            Id=Guid.NewGuid(),
                             OptionName="B",
                             OptionComment="选项内容B",
                        },
                        new {
                            Id=Guid.NewGuid(),
                             OptionName="C",
                             OptionComment="选项内容C",
                        }
                    }

                });
            }
            return this.JsonSuccess(new
            {
                Id = id,//考试ID
                PX_PlanId = Guid.NewGuid(),//培训计划ID
                PX_QuestionsList//考题列表
            });
        }


        /// <summary>
        /// 考试信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetExamInfo")]
        public IActionResult GetExamInfo(Guid id)
        {
            var retObj = new
            {
                Nameoftrainingplan = "培训名称",//培训名称
                Examinationstarttime = DateTime.Now,//考试开始时间
                Examinationscore = 60,//答题分数
                OkCount = 6,//正确数量
                ErrorCount = 3,//错误数量
                ErrorQuestions = new List<object>() {//错题集合
                    //PX_ExamUserQuestions
                    new {
                        QuestionContent="试题内容",//试题内容
                        CorrectOption="A",//正确选项
                        LastOption="B",//我的选项(最新选项)
                        //
                        PX_QuestionsOptions = new List<PX_QuestionsOptions>() {//考题选项
                        new PX_QuestionsOptions(){
                             OptionName="A",
                             OptionComment="选项内容A",
                             //IsAnswer="0"
                        },
                        new PX_QuestionsOptions(){
                             OptionName="B",
                             OptionComment="选项内容B",
                             //IsAnswer="0"
                        },
                        new PX_QuestionsOptions(){
                             OptionName="C",
                             OptionComment="选项内容C",
                             //IsAnswer="1"
                        }

                        }
                    },
                                     new {
                        QuestionContent="试题内容",//试题内容
                        CorrectOption="A",//正确选项
                        LastOption="B",//我的选项(最新选项)
                        //
                        PX_QuestionsOptions = new List<PX_QuestionsOptions>() {//考题选项
                        new PX_QuestionsOptions(){
                             OptionName="A",
                             OptionComment="选项内容A",
                             //IsAnswer="0"
                        },
                        new PX_QuestionsOptions(){
                             OptionName="B",
                             OptionComment="选项内容B",
                             //IsAnswer="0"
                        },
                        new PX_QuestionsOptions(){
                             OptionName="C",
                             OptionComment="选项内容C",
                             //IsAnswer="1"
                        }

                        }
                    }
                }
            };
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 提交考试
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost("Exam")]
        public IActionResult Exam(KaoShiViewModel viewModel)
        {
            return this.JsonSuccess();
        }


        /// <summary>
        /// 获取已答题信息
        /// </summary>
        /// <param name="ExamId">考试ID</param>
        /// <param name="type">0模拟1考试</param>
        /// <returns></returns>
        [HttpGet("GetMyAnswer")]
        public IActionResult GetMyAnswer(Guid ExamId, string type)
        {
            var questions = new List<object>();
            questions.Add(new
            {
                Id = Guid.Parse("a4799552-80a4-4831-9e1a-abf61652f018"),
                optionName = new string[] {
                            "A","B"
                        }
            }
            );
            questions.Add(new
            {
                Id = Guid.Parse("85cf8a14-79c9-45fa-bc40-c2dd584903fe"),
                optionName = new string[] {
                            "A","B"
                        }
            }
            );

            //for (int i = 0; i < 5; i++)
            //{
            //    questions.Add(new
            //    {
            //        Id = Guid.NewGuid(),
            //        optionName = new string[] {
            //                "A","B"
            //            }
            //    }
            //    );
            //}

            return this.JsonSuccess(questions);
        }

        #endregion


    }
}

